<template>
    <el-dialog
            title="权限管理"
            :visible.sync="dialogVisible"
            width="40%"
    >
        <div class="save_wrapper">
            <el-tree
                    :default-expand-all="true"
                    :data="datas"
                    show-checkbox
                    :check-strictly="true"
                    node-key="id"
                    @check="checkChange"
                    :default-checked-keys="checks"
                    :props="defaultProps">
            </el-tree>

        </div>
        <span slot="footer" class="dialog-footer">
             <el-button @click="dialogVisible = false" size="mini">取 消</el-button>
            <el-button type="primary" size="mini" @click="commit" :loading="isloading">确 定</el-button>
          </span>
    </el-dialog>
</template>

<script>
   import {saveRoleAuth,selectAuthority} from "../../../../api/base/role";

    export default {
        name: "authority",
        data(){
            return{
                formLabelWidth:'80px',
                checks:[],
                datas:[],
                roles:[],
                defaultProps: {
                    children: 'children',
                    label: 'menuName'
                },
                dialogVisible:false,
                isloading: false,
                selects:[],
                editform:{}
            }
        },
        methods:{
            setData(data){
                this.dialogVisible=true;
                this.checks=[];
                this.datas=[];
                this.selects=[];
                this.editform=data;
                selectAuthority(data).then(res=>{
                    if(res.status===0){
                        this.datas=res.data.all;
                        this.roles=res.data.role;
                        this.selects=this.roles;
                        if(res.data.role&&res.data.role.length>0){
                            res.data.role.forEach(item=>{
                                if(item.parentId||(item.parentId===0&&item.componentName)){
                                    this.checks.push(item.id)
                                }

                            });
                        }
                    }
                })
            },
            commit(){
                // return inventory(this.editForm);

                let ids=[];
                this.selects.forEach(item=>{
                    if(!ids.some(id=>item.id===id))
                        ids.push(item.id);
                    if(!ids.some(id=>item.parentId===id))
                        if (item.parentId)
                            ids.push(item.parentId)
                });
                saveRoleAuth({id:this.editform.id,ids:JSON.stringify(ids)}).then(res=>{
                     if(res.status===0){
                         this.$message.success("保存成功");
                     }else {
                         this.$message.error(res.msg);
                     }
                });
            },
            checkChange(node,checkedNodes){
                this.selects=checkedNodes.checkedNodes;
            }
        }
    }
</script>

<style scoped lang="sass">
    @import "../../../../styles/save"
</style>
